package org.jash.learnmvp.user;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputLayout;

import org.jash.learnmvp.R;
import org.jash.learnmvp.WeChatActivity;
import org.jash.learnmvp.mvp.BaseActivity;
import org.jash.learnmvp.utils.StoreUtils;

import java.util.regex.Pattern;

public class LoginActivity extends BaseActivity<ConStruct.ILoginView, LoginPresenter> implements ConStruct.ILoginView {

    private EditText username;
    private EditText password;
    private TextInputLayout phone;
    private String pattern = "(\\+86)?1[3589]\\d{9}";
    private CheckBox cb1;
    private CheckBox cb2;
    @Override
    protected int layoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        presenter = new LoginPresenter();
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        phone = findViewById(R.id.phone);
        cb1 = findViewById(R.id.cb1);
        cb2 = findViewById(R.id.cb2);
        findViewById(R.id.login).setOnClickListener(v -> {
//            if (phone.isErrorEnabled()) {
//                Toast.makeText(this, phone.getError(), Toast.LENGTH_SHORT).show();
//                return;
//            }
            presenter.login(username.getText().toString(), password.getText().toString());
        });
        phone.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                phone.setError("手机号不合法");
                phone.setErrorEnabled(!s.toString().matches(pattern));
            }
        });
        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                cb1.setChecked(s.toString().matches(".{8,}"));
                cb2.setChecked(s.toString().matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*$"));
            }
        });

    }

    @Override
    public void loginSuccess(String token) {
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        StoreUtils.getInstance().put("token", token);
        startActivity(new Intent(this, WeChatActivity.class));
        finish();
    }


    @Override
    public void loginFailure(Throwable throwable) {
        Toast.makeText(this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
        Log.e("TAG", "loginFailure: ", throwable);
    }
}